// elsePage/pages/xianShiTeHui/xianShiTeHui.js
const util = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    market_type:0,
    imgUrls: getApp().globalData.imgUrls,
    clicked: false
  },

  onUnload() {
    clearInterval(this.data.interval);
  },
  onLoad: function(options) {
    let market_type,title;
    if (options.title == '超值拼团' || options.qtTitle == '超值拼团'){
      market_type = 1;
        title='拼团列表'
    } else if (options.type == '限时秒杀' || options.qtTitle == '限时折扣'){
      market_type = 2;
      title = '限时秒杀'
    }
    wx.setNavigationBarTitle({
      title
    })
    this.setData({
      market_type,
      activity_id: options.activity_id || options.jump_id
    })
  },


  onShow: function() {
    this.getData()
  },
  jump(e) {
    let id = e.currentTarget.dataset.id,
      limit_id = e.currentTarget.dataset.limit_id || '',
      uniquekey = e.currentTarget.dataset.uniquekey;
    if (this.data.market_type == 1){
      wx.navigateTo({
        url: `/pages/groupShop/groupShop?goods_id=${id}&uniquekey=${uniquekey}&activity_id=${this.data.activity_id}`,
      })

    } else if (this.data.market_type == 2){
      wx.navigateTo({
        url: `/pages/seckill/seckill?goods_id=${id}&uniquekey=${uniquekey}&limit_id=${limit_id}`,
      })
    }
  },
  getData() {
    util.API('pyapi/Assemble', {
      market_type: this.data.market_type,
      activity_id: this.data.activity_id
    }, 'application/x-www-form-urlencoded').then(res => {
      let {
        code,
        data,
        msg
      } = res
      if (code == 200) {
        util.intervalTime(data.etime, this, 1000) // 倒计时
        let stime = util.getTimeDian(data.stime),
          etime = util.getTimeDian(data.etime);
        stime = `${stime.y}.${stime.m}.${stime.d}`
        etime = `${etime.y}.${etime.m}.${etime.d}`
        data.activitygoods=data.activitygoods.map(item=>{
          item.goods_cover = item.goods_cover.split(',')[0]
          return item
        })
        this.setData({
          limit_id: data.limit_id,
          activitygoods: data.activitygoods,
          pic: data.pic,
          status: data.status,
          content: data.content,
          stime,
          etime
        })
      } else {
        wx.showToast({
          title: msg + '',
          icon: 'none'
        })
      }
    })
  },
  changeClicked() {
    this.setData({
      clicked: !this.data.clicked
    })
  }
})